<template>
  <div class="applications-container">
    <div class="page-header">
      <h2 class="title">申请列表</h2>
      <div class="filter">
        <el-dropdown trigger="click">
          <span class="filter-btn">
            筛选类型 <i class="el-icon-arrow-down"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>全部类型</el-dropdown-item>
            <el-dropdown-item>预约看房</el-dropdown-item>
            <el-dropdown-item>申请租房</el-dropdown-item>
            <el-dropdown-item>合同续签</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>

    <div v-for="app in filteredApps" :key="app.id" class="app-card">
      <div class="card-header">
        <span class="app-type" :class="getTypeClass(app.type)">
          {{ app.type }}
        </span>
        <span class="status-tag" :class="getStatusClass(app.status)">
          {{ app.status }}
        </span>
      </div>

      <div class="card-content">
        <div class="app-info">
          <div class="info-item">
            <span class="label">房客：</span>
            <span>{{ app.tenant }}</span>
          </div>
          <div class="info-item">
            <span class="label">申请时间：</span>
            <span>{{ app.applyTime }}</span>
          </div>
          <div class="info-item" v-if="app.type === '申请租房'">
            <span class="label">租期：</span>
            <span>{{ app.leaseTerm }}</span>
          </div>
        </div>

        <div class="action-buttons" v-if="app.status === '待处理'">
          <el-button type="primary" size="small" @click="handleApp(app.id, '同意')">
            同意
          </el-button>
          <el-button type="danger" size="small" @click="handleApp(app.id, '拒绝')">
            拒绝
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

// 模拟申请数据（增加更多字段）
const applications = ref([
  {
    id: 'APP20231015001',
    type: '预约看房',
    tenant: '张三',
    applyTime: '2023-10-15 14:30',
    status: '待处理'
  },
  {
    id: 'APP20231016002',
    type: '申请租房',
    tenant: '李四',
    applyTime: '2023-10-16 10:00',
    leaseTerm: '12个月',
    status: '已同意'
  },
  {
    id: 'APP20231017003',
    type: '合同续签',
    tenant: '王五',
    applyTime: '2023-10-17 09:15',
    status: '待处理'
  }
])

// 筛选逻辑
const filterType = ref('')
const filteredApps = computed(() => {
  return filterType.value
      ? applications.value.filter(app => app.type === filterType.value)
      : applications.value
})

// 处理申请
const handleApp = (id, action) => {
  const appIndex = applications.value.findIndex(app => app.id === id)
  if (appIndex !== -1) {
    applications.value[appIndex].status = action === '同意' ? '已同意' : '已拒绝'
  }
}

// 状态标签样式映射
const getStatusClass = (status) => {
  return {
    'status-pending': status === '待处理',
    'status-approved': status === '已同意',
    'status-rejected': status === '已拒绝'
  }
}

// 类型标签样式映射
const getTypeClass = (type) => {
  return {
    'type-viewing': type === '预约看房',
    'type-rent': type === '申请租房',
    'type-renew': type === '合同续签'
  }
}
</script>

<style scoped>
.applications-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
  background-color: #fff;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
}

.title {
  font-size: 1.75rem;
  font-weight: 600;
  color: #1a1a1a;
}

.filter {
  display: flex;
  gap: 16px;
  align-items: center;
}

.filter-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 1rem;
  color: #606266;
  padding: 8px 16px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  cursor: pointer;
}

.app-card {
  padding: 24px;
  margin-bottom: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: transform 0.2s ease;
}

.app-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.app-type, .status-tag {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.875rem;
  font-weight: 500;
}

/* 类型标签颜色 */
.type-viewing { background-color: #e0f2f1; color: #00c851; }
.type-rent { background-color: #d0f5ff; color: #00bfff; }
.type-renew { background-color: #fff5d0; color: #ffb800; }

/* 状态标签颜色 */
.status-pending { background-color: #f8f9fa; color: #6c757d; }
.status-approved { background-color: #e0f2f1; color: #00c851; }
.status-rejected { background-color: #ffe4e1; color: #ff4444; }

.card-content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.app-info {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
  color: #606266;
}

.info-item {
  display: flex;
  gap: 8px;
}

.label {
  font-weight: 500;
  color: #303133;
  min-width: 80px;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .card-content {
    flex-direction: column;
    gap: 24px;
  }

  .app-info {
    grid-template-columns: 1fr;
  }
}
</style>